package com.btashov.stringarray;

/**
 * @author Bahtiyar Tashov <btashov@gmail.com>
 */
public class VerifyUniqueness {

    public static boolean isUniqueChars(String str) {
        boolean[] charSet = new boolean[256];
        for (int index = 0; index < str.length(); index++) {
            int value = str.charAt(index);                  //todo: why not char type
            if (charSet[value]) return false;
            charSet[value] = true;
        }
        return true;
    }

    public static boolean isUniqueCharsByBit(String str) {
        int checker = 0;
        for (int i = 0; i < str.length(); ++i) {
            int value = str.charAt(i);
            if ((checker & (1 << value)) > 0) return false;
            checker |= (1 << value);
        }
        return true;
    }

    public static boolean isUniqueCharsByBitRefactored(String str) {
        int checker = 0;

        for (int i = 0; i < str.length(); ++i) {
            int value = str.charAt(i);

            int newValue = 1 << value;
            int newChecker = checker & newValue;
            if (newChecker > 0) return false;
            checker |= (newValue);
        }
        return true;
    }
}
